/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.crosswall.lib.coverflow.view;

import me.crosswall.lib.coverflow.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * PagerContainerOne
 *
 * @since 2021-04-13
 */
public class PagerContainer extends DirectionalLayout {
    private Context context;
    private PageSlider pageSlider = null;
    private PageItemClickListener mListener;
    private float mF20 = 20f;

    /**
     * PagerContainerOne
     *
     * @param context q
     */
    public PagerContainer(Context context) {
        super(context);
        this.context = context;
        initview();
    }

    /**
     * PagerContainerOne
     *
     * @param context q
     * @param attrSet q
     */
    public PagerContainer(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        initview();
    }

    /**
     * PagerContainerOne
     *
     * @param context q
     * @param attrSet q
     * @param styleName q
     */
    public PagerContainer(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        initview();
    }

    private void initview() { // 要加入到当前的控件才不会报错
        ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_pageoneview, this, true);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_ViewPageSlider);
        pageSlider.setClipEnabled(false);

        componentContainer.setClipEnabled(false);
        pageSlider.setPageMargin(AttrHelper.vp2px(mF20, context));
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i2, float v1, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i1) {
            }

            @Override
            public void onPageChosen(int index) {
                if (mListener != null) {
                    mListener.onItemClick(index);
                }
            }
        });
    }

    /**
     * 获得pageslider
     *
     * @return PageSlider实列
     */
    public PageSlider getViewPager() {
        return pageSlider;
    }

    /**
     * 设置当前页数
     *
     * @param cut q
     */
    public void setCurrpage(int cut) {
        pageSlider.setCurrentPage(cut);
    }

    /**
     * 设置外部进行回调方法
     *
     * @param listener q
     */
    public void setPageItemClickListener(PageItemClickListener listener) {
        this.mListener = listener;
    }

    /**
     * 设置适配器和轮播的图片
     *
     * @param pageSliderAdapter 适配器
     */
    public void setPageadapter(PageSliderProvider pageSliderAdapter) {
        pageSlider.setProvider(pageSliderAdapter);
    }

    /**
     * 定义接口类
     *
     * @since 2021-04-13
     */
    public interface PageItemClickListener {
        /**
         * 回调当前的页数
         *
         * @param index q
         */
        void onItemClick(int index);

        /**
         * 得到当前数据
         *
         * @param i2 1
         * @param v1 2
         * @param i1 q
         */
        void onPageSliding(int i2, float v1, int i1);
    }
}
